/**
 * 
 */
/**
 * @author shanghaitao
 *
 */
package com.starbaba.system.userofgroup.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;
import org.springframework.stereotype.Component;

import com.starbaba.core.hibernate.DAOFactory;
import com.starbaba.core.hibernate.HibernateDAO;
import com.starbaba.core.service.CommonService;
import com.starbaba.core.service.CommonServiceSupport;
import com.starbaba.core.web.page.PageResponse;
import com.starbaba.system.userofgroup.model.UserOfGroupVO;
import com.starbaba.system.userofgroup.model.UserOfGroupModel;
import com.starbaba.system.userofgroup.service.UserOfGroupService;

@Component(value="UserOfGroupServiceImpl")
public class UserOfGroupServiceImpl  extends CommonServiceSupport implements UserOfGroupService {
	/**
	 * 
	 */
	private static final long serialVersionUID = -5108922671733677590L;
	
	@SuppressWarnings({ "rawtypes", "unchecked", "unused" })
    @Override
	public List<UserOfGroupVO> getDataList(int iDisplayStart, int iDisplayLength,
			UserOfGroupModel model, PageResponse pageResponse , CommonService server) {
		StringBuffer sf = new StringBuffer();
		sf.append("from UserOfGroupVO");
		String condition = bulidCondition(model,server);
		sf.append(condition);
		sf.append(" order by id asc");
		String hql = sf.toString();
		String countHql = "select count(*) " + hql;
		HibernateDAO dao = DAOFactory.getInstance().createHibernateDAO(UserOfGroupVO.class);
		Query query = dao.getSession().createQuery(hql);
		if(iDisplayLength != 0)
		{
			query.setFirstResult(iDisplayStart);
			query.setMaxResults(iDisplayLength);
		}
		//addTimeParm(model,query);
		List<UserOfGroupVO> list = query.list();
		List<UserOfGroupVO> returnList = new ArrayList<UserOfGroupVO>();
		UserOfGroupVO itemVo;
		SimpleDateFormat dateformat=new SimpleDateFormat("yyyy-MM-dd");
		Map<Long ,UserOfGroupVO> map = new HashMap<Long ,UserOfGroupVO>();
		for(UserOfGroupVO user :list)
		{
			itemVo = new UserOfGroupVO();
			itemVo.setGroupid(user.getGroupid());
            itemVo.setUserid(user.getUserid());
            itemVo.setId(user.getId());
            itemVo.setCreatetime(user.getCreatetime());
            itemVo.setCreator(user.getCreator());
			returnList.add(itemVo);
		}
		
		Query countQuery = dao.getSession().createQuery(countHql);
		Object obj = countQuery.uniqueResult();
		if(obj instanceof Long)
		{
			int count = ((Long)obj).intValue();
			
			pageResponse.setITotalRecords(count);
			pageResponse.setITotalDisplayRecords(count);
		}
		return returnList;
	}
	
	private String bulidCondition(UserOfGroupModel model, CommonService server) {
		StringBuffer condition = new StringBuffer();
		
		String userid = model.getUserid();
		if (!StringUtils.isEmpty(userid)) {
			condition.append(" where userid = '" + userid + "'");
		}
		
		return condition.toString();
	}
}
